package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/handler/resp"
	"github.com/gin-gonic/gin"
)

// 添加预约
func AddReservation(c *gin.Context) {
	var req request.AddReservationReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "参数有误", err.Error())
		return
	}

	addReservation, err := config.HouseClient.AddReservation(c, &__.AddReservationReq{
		CustomerId: req.CustomerId,
		CleanerId:  req.CleanerId,
		ClassType:  req.ClassType,
		Address:    req.Address,
		Area:       req.Area,
		StartTime:  req.StartTime,
		Duration:   req.Duration,
	})
	if err != nil {
		resp.Error(c, 500, "服务端错误", err.Error())
		return
	}

	resp.Success(c, "预约成功", addReservation.ReservationId)
}

// 订单创建
func AddOrder(c *gin.Context) {
	var req request.AddOrderReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "参数有误", err.Error())
		return
	}

	addOrder, err := config.HouseClient.AddOrder(c, &__.AddOrderReq{
		CustomerId:    req.CustomerId,
		CleanerId:     req.CleanerId,
		ReservationId: req.ReservationId,
	})
	if err != nil {
		resp.Error(c, 500, "服务端错误", err.Error())
		return
	}

	resp.Success(c, "订单创建成功", addOrder.OrderSn)
}

// 拒单
func RejectOrder(c *gin.Context) {
	var req request.RejectOrderReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "参数有误", err.Error())
		return
	}

	rejectOrder, err := config.HouseClient.RejectOrder(c, &__.RejectOrderReq{
		OrderId:   req.OrderId,
		CleanerId: req.CleanerId,
	})

	if err != nil {
		resp.Error(c, 500, "服务端错误", err.Error())
		return
	}

	resp.Success(c, "拒单成功", rejectOrder)
}

// 确认客户
func ConfirmCustomer(c *gin.Context) {
	var req request.ConfirmCustomerReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "参数有误", err.Error())
		return
	}

	confirmCustomer, err := config.HouseClient.ConfirmCustomer(c, &__.ConfirmCustomerReq{
		OrderId:   req.OrderId,
		CleanerId: req.CleanerId,
		Mobile:    req.Mobile,
	})
	if err != nil {
		resp.Error(c, 500, "服务端错误", err.Error())
		return
	}

	resp.Success(c, "客户确认成功", confirmCustomer.OrderId)
}

// 结束服务
func EndOrder(c *gin.Context) {
	var req request.EndOrderReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "参数有误", err.Error())
		return
	}

	endOrder, err := config.HouseClient.EndOrder(c, &__.EndOrderReq{
		OrderId:   req.OrderId,
		CleanerId: req.CleanerId,
	})
	if err != nil {
		resp.Error(c, 500, "服务端错误", err.Error())
		return
	}

	resp.Success(c, "服务结束成功", endOrder)
}

// 支付宝回调
func PayOrder(c *gin.Context) {
	r := c.Request

	orderSn := r.FormValue("out_trade_no")
	status := r.FormValue("trade_status")

	/*
	   WAIT_BUYER_PAY	交易创建，等待买家付款。
	   TRADE_CLOSED	未付款交易超时关闭，或支付完成后全额退款。
	   TRADE_SUCCESS	交易支付成功。
	   TRADE_FINISHED	交易结束，不可退款。
	*/

	if status == "WAIT_BUYER_PAY" {
		config.HouseClient.UpdatePayStatus(c, &__.UpdatePayStatusReq{
			OrderSn: orderSn,
			Status:  1, //交易创建，等待买家付款
		})
	} else if status == "TRADE_CLOSED" {
		config.HouseClient.UpdatePayStatus(c, &__.UpdatePayStatusReq{
			OrderSn: orderSn,
			Status:  2, //未付款交易超时关闭，或支付完成后全额退款
		})
	} else if status == "TRADE_SUCCESS" {
		config.HouseClient.UpdatePayStatus(c, &__.UpdatePayStatusReq{
			OrderSn: orderSn,
			Status:  3, //交易支付成功
		})
	} else if status == "TRADE_FINISHED" {
		config.HouseClient.UpdatePayStatus(c, &__.UpdatePayStatusReq{
			OrderSn: orderSn,
			Status:  4, //交易结束，不可退款
		})
	}

	w := c.Writer
	w.Write([]byte("success"))

}

// 订单列表
func GetOrderList(c *gin.Context) {
	var req request.GetOrderListReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "参数有误", err.Error())
		return
	}

	orderList, err := config.HouseClient.GetOrderList(c, &__.GetOrderListReq{
		CustomerId: req.CustomerId,
		Status:     req.Status,
	})
	if err != nil {
		resp.Error(c, 500, "服务端错误", err.Error())
		return
	}

	resp.Success(c, "列表展示成功", orderList)
}

// 订单详情
func GetOrderDetail(c *gin.Context) {
	var req request.GetOrderDetailReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "参数有误", err.Error())
		return
	}

	orderDetail, err := config.HouseClient.GetOrderDetail(c, &__.GetOrderDetailReq{
		OrderId: req.OrderId,
	})
	if err != nil {
		resp.Error(c, 500, "服务端错误", err.Error())
		return
	}

	resp.Success(c, "订单详情展示成功", orderDetail)
}
